home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / FILEPRNT.ICN < prev    next >
Text File  |  1992-11-26  |  3KB  |  102 lines

  1. ############################################################################
  2. #
  3. #    File:     fileprnt.icn
  4. #
  5. #    Subject:  Program to display characters in file
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     November 21, 1989
  10. #
  11. ###########################################################################
  12. #
  13. #     This program reads the file specified as a command-line argument and
  14. #  writes out a representation of each character in several forms:
  15. #  hexadecimal, octal, decimal, symbolic, and ASCII code.
  16. #
  17. #     Input is from a named file rather than standard input, so that it
  18. #  can be opened in untranslated mode.  Otherwise, on some systems, input
  19. #  is terminated for characters like ^Z.
  20. #
  21. #     Since this program is comparatively slow, it is not suitable
  22. #  for processing very large files.
  23. #
  24. #     There are several useful extensions that could be added to this program,
  25. #  including other character representations, an option to skip an initial
  26. #  portion of the input file, and suppression of long ranges of identical
  27. #  characters.
  28. #
  29. ############################################################################
  30. #
  31. #  Requires: co-expressions
  32. #
  33. ############################################################################
  34. #
  35. #  Program note:
  36. #
  37. #     This program illustrates a situation in which co-expressions can be
  38. #  used to considerably simplify programming.  Try recasting it without
  39. #  co-expressions.
  40. #
  41. ############################################################################
  42.  
  43. procedure main(arg)
  44.    local width, chars, nonprint, prntc, asc, hex, sym, dec
  45.    local oct, ascgen, hexgen, octgen, chrgen, prtgen, c
  46.    local cnt, line, length, bar, input
  47.  
  48.    input := open(arg[1],"u") | stop("*** cannot open input file")
  49.    width := 16
  50.    chars := string(&cset)
  51.    nonprint := chars[1:33] || chars[128:0]
  52.    prntc := map(chars,nonprint,repl(" ",*nonprint))
  53.  
  54.    asc := table("   |")
  55.    hex := table()
  56.    sym := table()
  57.    dec := table()
  58.    oct := table()
  59.    ascgen := create "NUL" | "SOH" | "STX" | "ETX" | "EOT" | "ENQ" | "ACK" |
  60.       "BEL" | " BS" | " HT" | " LF" |  " VT" | " FF" | " CR" | " SO" | " SI" |
  61.       "DLE" | "DC1" | "DC2" | "DC3" | "DC4" | "NAK" | "SYN" |  "ETB" | "CAN" |
  62.       " EM" | "SUB" | "ESC" | " FS" | " GS" | " RS" | " US" | " SP"
  63.    hexgen := create !"0123456789ABCDEF" || !"0123456789ABCDEF"
  64.    octgen := create (0 to 3) || (0 to 7) || (0 to 7)
  65.    chrgen := create !chars
  66.    prtgen := create !prntc
  67.    every c := !&cset do {
  68.       asc[c] := @ascgen || "|"
  69.       oct[c] := @octgen || "|"
  70.       hex[c] := " " || @hexgen || "|"
  71.       sym[c] := " " || @prtgen || " |"
  72.       }
  73.    asc[char(127)] := "DEL|"            # special case
  74.  
  75.    cnt := -1    # to handle zero-indexing of byte count
  76.  
  77.    while line := reads(input,width) do {    # read one line's worth
  78.       length := *line    # may not have gotten that many
  79.       bar := "\n" || repl("-",5 + length * 4)
  80.       write()
  81.       writes("BYTE|")
  82.       every writes(right(cnt + (1 to length),3),"|")
  83.       write(bar)
  84.       writes(" HEX|")
  85.       every writes(hex[!line])
  86.       write(bar)
  87.       writes(" OCT|")
  88.       every writes(oct[!line])
  89.       write(bar)
  90.       writes(" DEC|")
  91.       every writes(right(ord(!line),3),"|")
  92.       write(bar)
  93.       writes(" SYM|")
  94.       every writes(sym[!line])
  95.       write(bar)
  96.       writes(" ASC|")
  97.       every writes(asc[!line])
  98.       write(bar)
  99.       cnt +:= length
  100.       }
  101. end
  102.